package SiTuQingYou.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import SiTuQingYou.dao.UserDao;
import SiTuQingYou.vo.User;

/**
 * Servlet implementation class LookForPasswordServlet
 */
@WebServlet("/LookForPasswordServlet")
public class LookForPasswordServlet extends BaseAction {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LookForPasswordServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	

	
	protected void toLookForPassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username = request.getParameter("username");
		
		String question = UserDao.toLookForPassword(username);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/txt;charset=UTF-8");
		PrintWriter out = response.getWriter();
		if(question!=null)
		{
			out.print(question);
		}
		else
		{
			out.print("no");
		}
		
	}
	
	protected void toCheckAnswer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String answerinput = request.getParameter("answer");
		String avaiUsername = request.getParameter("name");
		
		boolean f = UserDao.toCheckAnswer(answerinput,avaiUsername);
		PrintWriter out = response.getWriter();
		out.print(f);
	}
	
	protected void toChangePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username = request.getParameter("pleaseUsername");
		String password = request.getParameter("pleasePassword");
		
		UserDao.toChangePassword(username,password);
		
		String path = request.getContextPath();
		response.sendRedirect(path+"/anotherLogReg/login.jsp?z=1");
		
		
	}
	
	//修改
	protected void toRevise(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		String newpassword = request.getParameter("newpassword");
		UserDao.getud().revisePassword(user.getId(),newpassword);
		
		String path = request.getContextPath();
		response.sendRedirect(path+"/anotherLogReg/login.jsp?rev=1");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr
		
	}
}
